package com.scu.qingshi.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.eventbus.Subscribe;
import com.scu.qingshi.config.cummunity.CommunityConfig;
import com.scu.qingshi.config.cummunity.CommunityFactory;
import com.scu.qingshi.domain.email.enums.EmailStatusEnum;
import com.scu.qingshi.domain.interview.event.NodeModifyEvent;
import com.scu.qingshi.infrastructure.dao.po.*;
import com.scu.qingshi.infrastructure.dao.service.EmailService;
import com.scu.qingshi.infrastructure.dao.service.InterUserMapService;
import com.scu.qingshi.infrastructure.dao.service.InterviewProcessService;
import com.scu.qingshi.infrastructure.dao.service.UserService;
import com.scu.qingshi.infrastructure.event.EventBusCenter;
import com.scu.qingshi.infrastructure.utils.EmailUtil;
import com.scu.qingshi.ws.UserWebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qingshi
 */
@Component
public class ModifyNodeEventListener {

    @Autowired
    EventBusCenter eventBusCenter;

    @Autowired
    EmailUtil emailUtil;

    @Autowired
    InterUserMapService interUserMapService;

    @Autowired
    EmailService emailService;

    @Autowired
    InterviewProcessService interviewProcessService;

    @Autowired
    UserService userService;

    @PostConstruct
    void init(){
        eventBusCenter.register(this);
    }

    // 通知对象([参加该面试]&&[下一面为修改的节点])节点信息修改
    @Subscribe
    void listenNodeModify(NodeModifyEvent event){
        QueryWrapper<InterUserMap> wrapper = new QueryWrapper<>();
        wrapper.select("user_id")
                .eq("inter_id",event.getInterId())
                .eq("cur_node_id",event.getNodeId())
                .eq("pass",1)
                .eq("success",0);
        List<Integer> userIds = interUserMapService.getBaseMapper().selectList(wrapper)
                .stream()
                .map(InterUserMap::getUserId).collect(Collectors.toList());

        CommunityConfig community = CommunityFactory.getCommunity(event.getOpenId());

        InterviewProcess process  = interviewProcessService.getById(event.getInterId());

        String message = "您参加的面试["+process.getName()+"]的下一面内容有所变动，请及时关注";

        userIds.forEach(id -> {
            try {
                // 实时通知在线用户
                UserWebSocketServer.sendInfo(message,String.valueOf(id));
                // 异步发送邮件
                emailService.save(Email.builder()
                                .status(EmailStatusEnum.INIT.name())
                                .content(message)
                                .fromEmail(community.getEmail())
                                .toEmail(userService.getById(id).getEmail())
                                .title("来自社团："+community.getName()+"的通知")
                                .interId(event.getInterId())
                                .openId(event.getOpenId())
                                .build());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

}
